import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { Gender, TreasuryCheckin, TreasuryGoods, TreasuryPrize, TreasuryRole, TreasuryStatus } from "@/types";
import { TaskOwner, TaskLimitType, TaskType } from "@/constants";

export interface TreasuryBanner {
  treasuryBannerId: number; // banner id
  image: string; // 图片
}

export interface UserTreasury {
  userTreasuryId: number; // 用户宝库id

  userId: number; // 用户id
  treasuryId: number; // 宝库id

  userScore: number; // 用户积分
  username: string; // 用户名
  userAvatar: string; // 用户头像
  userGender: Gender; // 用户性别

  treasuryRole: TreasuryRole; // 用户角色
  treasuryName : string; // 宝库名
  treasuryLogo : string; // 宝库logo
  treasuryDescription : string; // 宝库描述
  treasuryStatus : TreasuryStatus; // 宝库状态
  treasuryOwnerId : number; // 宝库拥有者id
  treasuryOwnerName : string; // 宝库拥有者名字
  treasuryBannerList: TreasuryBanner[]; // 宝库banner
  treasuryPicture: string; // 个人页顶部图片
  treasuryTotalScore: number; // 宝库总积分
  treasuryCheckin: TreasuryCheckin; // 签到情况
}

export interface TreasuryTask {
  taskId: number; // 任务id

  treasuryId: number; // 宝库id

  name: string;  // 任务标题
  description: string; // 任务描述
  owner: TaskOwner; // 发布人
  score: number; // 任务奖励积分
  
  type: TaskType; // 任务类型
  assignee: string; // 指定情况
  limitType: TaskLimitType; // 限制情况
  deadline: string; // 截止时间
}

interface Treasury {
  userTreasury: UserTreasury;
  taskList?: TreasuryTask[];
  goodsList?: TreasuryGoods[];
  prizeList?: TreasuryPrize[];
}

const treasurySlice = createSlice({
  name: 'treasury',
  initialState: {} as Treasury,
  reducers: {
    setTreasury: (state, action: PayloadAction<Treasury>) => {
      return { ...state, ...action.payload }
    },
    setUserTreasury: (state, action: PayloadAction<UserTreasury>) => {
      return { ...state, userTreasury: action.payload }
    },
    setTreasuryTaskList: (state, action: PayloadAction<TreasuryTask[]>) => {
      return { ...state, taskList: action.payload }
    },
    setTreasuryGoodsList: (state, action: PayloadAction<TreasuryGoods[]>) => {
      return { ...state, goodsList: action.payload }
    },
    setTreasuryPrizeList: (state, action: PayloadAction<TreasuryPrize[]>) => {
      return { ...state, prizeList: action.payload }
    },
    clearTreasury: () => {}
  }
})

export const {
  setTreasury,
  setUserTreasury,
  setTreasuryTaskList,
  setTreasuryGoodsList,
  clearTreasury,
  setTreasuryPrizeList
} = treasurySlice.actions

export default treasurySlice.reducer